package com.shop.service.shop.controller;

import com.shop.service.module.entity.OrderEntity;
import com.shop.service.module.entity.Result;
import com.shop.service.module.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @GetMapping("/list/page")
    @Cacheable(value = "orderList", condition = "true")
    public Result getListPage(@RequestParam(value = "pno", defaultValue = "1") Integer pno,
                              @RequestParam(value = "psize", defaultValue = "10") Integer psize,
                              @RequestParam(value = "status", required = false) Integer status,
                              @RequestParam(value = "orderNo", required = false) String orderNo,
                              @RequestParam(value = "type", required = false) Integer type,
                              @RequestParam(value = "phone", required = false) String phone) {
        return orderService.getListPage(pno, psize, status, orderNo, type, phone);

    }

    @DeleteMapping("/delete/id/{id}")
    @CacheEvict(value = "orderList", allEntries = true)
    public Result delete(@PathVariable Long id) {
        boolean result = orderService.removeById(id);
        return result ? Result.end(200, "", "删除成功") : Result.end(500, "", "删除失败");

    }
    @GetMapping("find/id/{id}")
    public Result getOrderById(@PathVariable Long id){
        OrderEntity orderEntity = orderService.getById(id);
        return Result.end(200,orderEntity,"查询成功");

    }
    @GetMapping("/send")
    @CacheEvict(value = "orderList", allEntries = true)
    public Result orderSend(@RequestParam Long id,
                            @RequestParam String postCode){
        return orderService.sendOrder(id,postCode);
    }


}
